/*
 * Alberto Ruiz-Larrea Guillén...11383
 * Erdi Dogruel..................14955
 * Marcos Pérez Martín...........10324
 */

package com.example.database;

import java.util.HashMap;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Log;

//This class generates the gameplan
public class InsertGamePlan extends AsyncTask<OpenDB, Integer, String> {

	@Override
	protected String doInBackground(OpenDB... params) {

		SQLiteDatabase read = params[0].getReadableDatabase();
		String[] columns = new String[] { OpenDB.id, OpenDB.name,
				OpenDB.rating, OpenDB.selected };
		Cursor cursor = read.query(OpenDB.table1, columns, null, null, null,
				null, null);
		cursor.moveToFirst();

		SQLiteDatabase write = params[0].getWritableDatabase();
		ContentValues values = new ContentValues();

		HashMap<Integer, String> hash = new HashMap<Integer, String>();
		int team_size = 0;
		int z = 1;

		while (!cursor.isAfterLast()) {
			team_size++;
			hash.put(team_size, cursor.getString(1));
			cursor.moveToNext();
		}
		cursor.close();

		for (int i = 1; i <= (team_size - 1); i++) {
			values.put(OpenDB.game_home, hash.get(i));
			values.put(OpenDB.game_away, hash.get(team_size));
			values.put(OpenDB.score_home, -1);
			values.put(OpenDB.score_away, -1);
			values.put(OpenDB.gameday, z);
			write.insert(OpenDB.table3, null, values);

			for (int k = 1; k <= ((team_size / 2) - 1); k++) {

				int t1 = (((i + k) % (team_size - 1)) + (team_size - 1))
						% (team_size - 1);
				int t2 = (((i - k) % (team_size - 1)) + (team_size - 1))
						% (team_size - 1);
				if (t1 == 0) {
					t1 = (team_size - 1);
				}
				if (t2 == 0) {
					t2 = (team_size - 1);
				}

				values.put(OpenDB.game_home, hash.get(t1));
				values.put(OpenDB.game_away, hash.get(t2));
				values.put(OpenDB.score_home, -1);
				values.put(OpenDB.score_away, -1);
				values.put(OpenDB.gameday, z);
				write.insert(OpenDB.table3, null, values);

			}
			z++;

		}

		for (int i = 1; i <= (team_size - 1); i++) {
			values.put(OpenDB.game_home, hash.get(team_size));
			values.put(OpenDB.game_away, hash.get(i));
			values.put(OpenDB.score_home, -1);
			values.put(OpenDB.score_away, -1);
			values.put(OpenDB.gameday, z);
			write.insert(OpenDB.table3, null, values);

			for (int k = 1; k <= ((team_size / 2) - 1); k++) {

				int t1 = (((i + k) % (team_size - 1)) + (team_size - 1))
						% (team_size - 1);
				int t2 = (((i - k) % (team_size - 1)) + (team_size - 1))
						% (team_size - 1);
				if (t1 == 0) {
					t1 = (team_size - 1);
				}
				if (t2 == 0) {
					t2 = (team_size - 1);
				}

				values.put(OpenDB.game_home, hash.get(t2));
				values.put(OpenDB.game_away, hash.get(t1));
				values.put(OpenDB.score_home, -1);
				values.put(OpenDB.score_away, -1);
				values.put(OpenDB.gameday, z);
				write.insert(OpenDB.table3, null, values);

			}
			z++;

		}

		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		if (result == "-1") {
			Log.e("Insert", "Error while insert gameplan in DB");
		}
	}
}